// pages/find/find.js
import { request } from '../../api/api'
function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length === 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toggle: false,
    percent: 100,
    banners: [],
    poster: 'https://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg',
    name: '多余的解释',
    author: '许嵩',
    srcOne: "https://music.163.com/song/media/outer/url?id=167873.mp3",
    srcTwo: "https://music.163.com/song/media/outer/url?id=145586.mp3",
    vsrc: "http://47.108.225.10:3333/music/Midnights/TaylorSwift_LanaDelRey-SnowOnTheBeach(Explicit).mp3",
    src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',
    src3: "https://mclaren.scene7.com/is/content/mclaren/McLaren%20720s%20Super%20Series-0x720-3000k",
    danmuList:
      [{
        text: '第 1s 出现的弹幕',
        color: '#ff0000',
        time: 1
      }, {
        text: '第 3s 出现的弹幕',
        color: '#ff00ff',
        time: 3
      }],
    word: 666
  },
  showAlert() {
    this.videoCtx.pause()
    wx.showModal({
      title: '友情提示',
      content: '亲爱的,你正在使用网络流量观看视频?',
      showCancel: true,
      cancelText: '取消观看',
      cancelColor: '#000000',
      confirmText: '确定观看',
      confirmColor: '#3CC51F',
      success: async (result) => {
        if (result.confirm) {
          wx.setStorageSync('isPlay', true)
          this.videoCtx.play()
        }
      },
      fail: () => { },
      complete: () => { }
    });
  },
  videoplay() {
    console.log('开始播放')
    if (!wx.getStorageSync('isPlay')) {
      this.showAlert()
    }
  },
  changeSrc() {
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: ['front', 'back'],
      success: (res) => {
        this.setData({
          src3: res.tempFilePath
        })
      }
    })
  },
  startSendDanmu() {
    this.videoCtx.sendDanmu({
      text: this.data.word,
      color: getRandomColor()
    })
  },
  audioPlay() {
    this.audioCtx.play()
  },
  audioPause() {
    this.audioCtx.pause()
  },
  audioPlay2() {
    this.myAudio.play()
  },
  audioPause2() {
    this.myAudio.pause()
  },
  async getBannerData() {
    let res = await request({
      url: "http://121.196.235.163:3000/banner"
    })
    this.setData({
      banners: res.data.banners.slice(0, 3)
    })
  },
  timeDown() {
    this.setData({
      toggle: true
    })
    this.getBannerData()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.audioCtx = wx.createAudioContext('myAudio')  // 得到audio对象 
    this.myAudio = wx.createInnerAudioContext()
    this.myAudio.src = this.data.srcTwo;
    // this.myAudio.play()

    this.videoCtx = wx.createVideoContext('myVideo')

    // 判断网络状态 
    wx.getNetworkType({
      success(res) {
        const networkType = res.networkType
        console.log(networkType)

        wx.setStorageSync("isPlay", networkType == 'wifi')   // Sync 同步 
      }
    })

    wx.onNetworkStatusChange(function (res) {
      // console.log(res.isConnected)
      // console.log(res.networkType)
      wx.setStorageSync("isPlay", res.networkType == 'wifi')
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})